import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.sql.*;

public class editPatientFrame extends JDialog
{
 
  /* Frame */
  editPatientFrame frame;
  Canvas canvas;
  JPanel panel;
  JTextField firstName;
  JTextField lastName;
  JTextField birthday;
  JComboBox gender;
  JComboBox maritalStatus;
  JTextField nationality;
  JTextField contactNumbers;
  JLabel title;
  JButton btnSaveChanges;
  JButton btnCancel;
  
  JLabel backgroundImage;
  
  public editPatientFrame(JFrame frame, String str){
            super(frame, str);
            addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent evt){
                    MainFrame.frame.setEnabled(true);
                }
            });
    }
    
  public void MainFrame(){
      
        /* Frame Options */
        frame = new editPatientFrame(new JFrame(),"Edit Patient Information");
        frame.setSize(370,360);

        /* Panel Options*/
        panel = new JPanel();
        panel.setPreferredSize(new Dimension(370, 370));
        panel.setLayout(null);
        
            title = new JLabel("Edit Patient Information ");
            title.setFont(new Font("Microsoft Sans Serif", Font.PLAIN, 16));
            title.setForeground(Color.white);
            title.setBounds(20,0,270,30);
            panel.add(title);
        
            title = new JLabel("First Name: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,20,170,50);
            panel.add(title);
       
            title = new JLabel("Last Name: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,55,170,50);
            panel.add(title);
       
            title = new JLabel("Birthday: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,90,250,50);
            panel.add(title);
            
            title = new JLabel("Gender: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,125,250,50);
            panel.add(title);
            
            title = new JLabel("Marital Status: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,160,250,50);
            panel.add(title);
            
            title = new JLabel("Nationality: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,195,250,50);
            panel.add(title);
            
            title = new JLabel("Contact Number/s: ");
            title.setFont(new Font("Cambria", Font.PLAIN, 14));
            title.setForeground(Color.black);
            title.setBounds(20,230,250,50);
            panel.add(title);
            
            try{
              // Get a statement from the connection
              MainFrame.database.stmt = MainFrame.database.conn.createStatement();
              // Execute the query
                               
              MainFrame.database.rs = MainFrame.database.stmt.executeQuery("SELECT * FROM PATIENT WHERE PATIENTID = "+ patientsPage.picked );
                               
               while( MainFrame.database.rs.next() ){
                   
                /* Last Name input */
                lastName = new JTextField(MainFrame.database.rs.getString("lastName"));
                lastName.setBounds(150,70,190,25);
                lastName.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                panel.add(lastName);
                /* First Name input */
                firstName = new JTextField(MainFrame.database.rs.getString("firstName"));
                firstName.setBounds(150,35,190,25);
                firstName.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                panel.add(firstName);
                
                /* Bday input */
                birthday = new JTextField(MainFrame.database.rs.getString("birthDate"));
                birthday.setBounds(150,105,190,25);
                birthday.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                panel.add(birthday);
                           
                /* Gender Combobox */
                gender = new JComboBox();
                gender.addItem("Male");
                gender.addItem("Female");
                gender.setBackground(Color.WHITE);
                gender.setBounds(150,140,190,25);
                if(MainFrame.database.rs.getString("gender").equals("Male"))
                    gender.setSelectedIndex(0);
                else
                    gender.setSelectedIndex(1);
                gender.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                panel.add(gender);
                
                /* Contact number/s input */
                contactNumbers = new JTextField(MainFrame.database.rs.getString("contactNum"));
                contactNumbers.setBounds(150,245,190,25);
                contactNumbers.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                panel.add(contactNumbers);
                
                 /* Marital Status Combobox */
                maritalStatus = new JComboBox();
                maritalStatus.addItem("Single");
                maritalStatus.addItem("Married");
                maritalStatus.setBackground(Color.WHITE);
                maritalStatus.setBounds(150,175,190,25);
                if(MainFrame.database.rs.getString("maritalStatus").equals("Single"))
                    maritalStatus.setSelectedIndex(0);
                else
                    maritalStatus.setSelectedIndex(1);
                maritalStatus.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                panel.add(maritalStatus);
            
                /* Nationality input */
                nationality = new JTextField(MainFrame.database.rs.getString("nationality"));
                nationality.setBounds(150,210,190,25);
                nationality.setFont(new java.awt.Font("Cambria",java.awt.Font.PLAIN,14));
                panel.add(nationality);
            
       
                }
            } catch(Exception e){}

            /* Save Changes Button */
            btnSaveChanges = new JButton(new ImageIcon("saveChangesButton.jpg"));
            btnSaveChanges.setBounds(50,290,114,25);
            btnSaveChanges.addActionListener(new ButtonListener());
            panel.add(btnSaveChanges);
            
            /* Cancel Button */
            btnCancel = new JButton(new ImageIcon("cancelButton.jpg"));
            btnCancel.setBounds(174,290,114,25);
            btnCancel.addActionListener(new ButtonListener());
            panel.add(btnCancel);
            
            
            /* Background Image */
            backgroundImage = new JLabel(new ImageIcon("bgedit.jpg"));  
            backgroundImage.setBounds(0,0,370,370);
            panel.add(backgroundImage);
        
            /* Frame Operations */
            frame.getContentPane().add(panel);
            frame.setLocationRelativeTo(null);
            frame.setResizable(false);
            frame.setVisible(true);
  }
  
    public class ButtonListener implements ActionListener{

     public void actionPerformed(ActionEvent e){
       if(e.getSource() == btnSaveChanges){
             try{
                        MainFrame.database.stmt = MainFrame.database.conn.createStatement();
                        
                                 int numUpdated = 
                                 MainFrame.database.stmt.executeUpdate( "UPDATE PATIENT SET lastName = '" + lastName.getText() +
                                 "', firstName = '" + firstName.getText() + "', birthDate = '" + birthday.getText() + "', gender = '" + 
                                 (String) gender.getSelectedItem() + "', contactNum = '" + contactNumbers.getText() +
                                 "', maritalStatus = '" + (String) maritalStatus.getSelectedItem() + "', nationality = '" + 
                                 nationality.getText() + "' WHERE patientId = " + patientsPage.picked);
                                 
                                 
                                 JOptionPane.showMessageDialog(null,"All changes made are successfully saved.");
           
                                // frame.dispose();
           
                                 MainFrame.panel.removeAll();
                                 MainFrame.patientsPage.Draw();
                                 MainFrame.header.Draw();
                                 MainFrame.panel.add(MainFrame.backgroundImage);
                                 MainFrame.panel.repaint();
            
                               // MainFrame.frame.setEnabled(true);
                                // MainFrame.frame.toFront();
                                // MainFrame.frame.setState(Frame.NORMAL);
                                 
               }catch(SQLException se){}
               
        }
       else if(e.getSource() == btnCancel){
           frame.dispose();
           MainFrame.frame.setEnabled(true);
           MainFrame.frame.toFront();
           MainFrame.frame.setState(Frame.NORMAL);
        }
    }
   }
  }  

